/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upc.clase.demo.dao.impl;

import edu.upc.clase.demo.dao.SalaDao;
import edu.upc.clase.demo.entity.Sala;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;
/**
 *  private String codSala;
    private String nomSala;
    private Integer capacsala;
    private Image imagSala; 
    private Double costoSala;
    private String codLocal;
    private String codAcces;
    private String codInst;
    private String codSerAdic;
 * @author usuario
 */
@Repository
public class SalaDaoImpl extends SimpleJdbcDaoSupport implements SalaDao{
  private static Logger log = LoggerFactory.getLogger(LocalDaoImpl.class);
  
      @Autowired
       public SalaDaoImpl (DataSource dataSource) {
       log.info("Asignando el dataSource");
       setDataSource(dataSource);    
    }
      @Override
      public Integer insertar(Sala sala) {
        getJdbcTemplate().update(
                "insert into Sala (codSala,nomSala,capacsala,imagSala,costoSala,"
                + "codLocal,codAcces,codInst,codSerAdic) "
                + "values (?, ?, ?,?, ?, ?,?, ?, ?)",
                sala.getCodSala(),sala.getNomSala(),sala.getCapacsala(),sala.getImagSala(),
                sala.getCostoSala(),sala.getCodLocal(),sala.getCodAcces(),sala.getCodInst(),
                sala.getCodSerAdic());
        return getSimpleJdbcTemplate().queryForInt("select last_insert_codSala()");
    }
      @Override
      public void actualizar(Sala sala) {
        getJdbcTemplate().update(
                "update Sala set  nomSala= ?, capacsala= ?, imagSala= ?, costoSala= ?,"
                + " codLocal= ?, codAcces= ?, codInst= ?, codSerAdic= ? where codSala = ?",
                sala.getNomSala(),sala.getCapacsala(),sala.getImagSala(),
                sala.getCostoSala(),sala.getCodLocal(),sala.getCodAcces(),sala.getCodInst(),
                sala.getCodSerAdic(),sala.getCodSala());
    }

    @Override
    public void eliminar(Sala sala) {
        getJdbcTemplate().update(
                "delete from Sala where codSala = ?",sala.getCodSala());
    }
    @Override
    public Sala buscar(String nombSala) {
        try {
            return getSimpleJdbcTemplate().queryForObject(
                    "select nomSala, capacsala, imagSala, costoSala,"
                    + " codLocal, codAcces, codInst, codSerAdic from Sala where nombSala=?",
                    new BeanPropertyRowMapper<Sala>(Sala.class), nombSala);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public List<Sala> buscarTodos() {
        return getSimpleJdbcTemplate().query(
                    "select nomSala, capacsala, imagSala, costoSala,"
                    + " codLocal, codAcces, codInst, codSerAdic from Sala",
                new BeanPropertyRowMapper<Sala>(Sala.class));
    }
}

